package com.example.web.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQMapMessage;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.jms.Destination;
import java.io.Serializable;
import java.util.Date;

/**
 * 队列模式提供者
 */
@Component
@Slf4j
public class QueueProducer {

	/**
	 * MQ jms实例
	 **/
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	/**
	 * 之前遇到一个问题，想要订阅模式 ，始终不成功，原因是在发送的时候，new ActiveMQQueue(queueName);是队列而不是主题。
	 * 查阅各种资料最后才知道这他妈坑在这里。
	 * @param queueName
	 * @param message
	 */
	//发送map 这里new ActiveMQTopic(queueName);可以实现订阅者模式。
	public void sendMapMessage(String queueName, Object message) {
		threadPoolTaskExecutor.submit(() -> {
			try {
				Destination destination = new ActiveMQTopic(queueName);
				// 这里定义了Queue的key
				ActiveMQMapMessage mqMapMessage = new ActiveMQMapMessage();
				mqMapMessage.setJMSDestination(destination);
				mqMapMessage.setObject("result", message);
				this.jmsMessagingTemplate.convertAndSend(destination, mqMapMessage);
			} catch (Throwable e) {
				log.error("{}", e);
			}
		});
	}
	//发送对象类型
	public void sendObjectMessage(String queueName, Object message) {
		threadPoolTaskExecutor.submit(() -> {
			try {
				log.info("发送添加好友请求:{}", message.toString());
				Destination destination = new ActiveMQQueue(queueName);
				// 这里定义了Queue的key
				ActiveMQObjectMessage mqObjectMessage = new ActiveMQObjectMessage();
				mqObjectMessage.setJMSDestination(destination);
				mqObjectMessage.setObject((Serializable) message);
				this.jmsMessagingTemplate.convertAndSend(destination, mqObjectMessage);
			} catch (Throwable e) {
				log.error("{}", e);
			}
		});
	}
	//发送公用的发送队列的方式
	/*
	 * 这里是Destination 对象， 可以是 queue，也可以是topic
	 */
	public void sendObjectMessage(Destination destination, Object message) {
		threadPoolTaskExecutor.submit(() -> {
			Date date = new Date();
			try {
				// 这里定义了Queue的key
				log.info("【queue-->send】:activeCount={},queueCount={},completedTaskCount={},taskCount={}",
						threadPoolTaskExecutor.getThreadPoolExecutor().getActiveCount(),
						threadPoolTaskExecutor.getThreadPoolExecutor().getQueue().size(),
						threadPoolTaskExecutor.getThreadPoolExecutor().getCompletedTaskCount(),
						threadPoolTaskExecutor.getThreadPoolExecutor().getTaskCount());

				ActiveMQObjectMessage mqObjectMessage = new ActiveMQObjectMessage();
				mqObjectMessage.setJMSDestination(destination);
				mqObjectMessage.setObject((Serializable) message);
				this.jmsMessagingTemplate.convertAndSend(destination, mqObjectMessage);
			} catch (Throwable e) {
				log.error("{}", e);
			}
		});
	}

}
